import os, shutil

from PySide6.QtGui import QIcon, QAction
from PySide6.QtWidgets import QToolBar

from .ui.autoinput import AutoInputWidget
from .ui.fill_category import FillCategoryWidget
from .ui.fill_columns import FillColumnsWidget
from .ui.gen_frees import GenerateFreesWidget
from .ui.gen_tasks import GenTasksWidget

CurrDir = os.path.dirname(__file__)

class ExcelPlugin:
    def __init__(self, main_window):
        self.host = main_window
        self.filename = os.path.join(os.path.expanduser('~'), 'Desktop/temp.txt')

        self.InitActions()

        self.autoinput_widget = AutoInputWidget(parent=self)
        self.autoinput_widget.filename = self.filename

        self.generate_account_widget = FillCategoryWidget()
        self.generate_account_widget.filename = self.filename

        self.generate_frees_widget = GenerateFreesWidget()
        self.generate_frees_widget.filename = self.filename

        self.fill_columns_widget = FillColumnsWidget()
        self.fill_columns_widget.filename = self.filename

        self.gen_task_widget = GenTasksWidget()
        self.gen_task_widget.filename = self.filename

    def InitActions(self):
        self.autoinputAction = self.CreateAction("自动录入 &A", self.AutoInput)
        self.generateAccountAction = self.CreateAction(
            "生成账目 &G", self.GenerateAccount
        )
        self.generateFreesAction = self.CreateAction(
            "生成免费申报 &F", self.GenerateFrees
        )
        self.fillColumnsAction = self.CreateAction("填充列 &C", self.FillColumns)
        self.genTaskAction = self.CreateAction('生成任务表 &T', self.GenTask)

        self.workActions = (
            self.autoinputAction,
            self.generateAccountAction,
            self.generateFreesAction,
            self.genTaskAction,
            self.fillColumnsAction,
        )

    def AutoInput(self):
        self.autoinput_widget.show()
        if os.path.exists(self.autoinput_widget.filename):
            self.host.excel_filename = self.autoinput_widget.filename
            

    def GenerateAccount(self):
        self.generate_account_widget.show()
        if os.path.exists(self.generate_account_widget.filename):
            self.host.excel_filename = self.generate_account_widget.filename

    def GenerateFrees(self):
        self.generate_frees_widget.show()
        if os.path.exists(self.generate_frees_widget.filename):
            self.host.excel_filename = self.generate_frees_widget.filename

    def FillColumns(self):
        self.fill_columns_widget.show()
        if os.path.exists(self.fill_columns_widget.filename):
            self.host.excel_filename = self.fill_columns_widget.filename

    def GenTask(self):
        self.gen_task_widget.show()
        if os.path.exists(self.gen_task_widget.filename):
            self.host.excel_filename = self.gen_task_widget.filename

    def CreateAction(
        self,
        text,
        slot=None,
        shortcut=None,
        icon=None,
        tip=None,
        checkable=False,
        signal="triggered",
    ):
        action = QAction(text, self.host)  # 此处需使用 self.host， 否则无效
        actSignal = None
        if signal == "triggered":
            actSignal = action.triggered
        elif signal == "toggled":
            actSignal = action.toggled
        elif signal == "changed":
            actSignal = action.changed
        elif signal == "hovered":
            actSignal = action.hovered
        else:
            actSignal = action.triggered

        if icon is not None:
            iconpath = os.path.join(CurrDir, "images/" + icon)
            action.setIcon(QIcon(iconpath))
        else:
            action.icon = None

        if shortcut is not None:
            action.setShortcut(shortcut)
        if tip is not None:
            action.setToolTip(tip)
        if slot is not None:
            actSignal.connect(slot)
        if checkable:
            action.setCheckable(checkable)
        return action

    def AddActions(self, target, actions):
        for action in actions:
            if action is None:
                if not isinstance(target, QToolBar):
                    target.addSeparator()
            elif isinstance(target, QToolBar) and action.icon is None:
                continue
            else:
                target.addAction(action)



if __name__ == '__main__':
    #RenameUi()
    pass 